# -*- coding: utf-8 -*-
import requests

# 设置控制台编码（Windows系统）： 在Windows系统中，可以通过以下代码设置控制台编码为UTF-8：
import sys,io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

def fetch_resource_with_cache(url):
    headers = {}
    # 假设本地记录的上次修改时间
    last_modified = "Wed, 01 Jan 2024 12:00:00 GMT"
    headers["If-Modified-Since"] = last_modified
    cached_tag = 'abc123'
    headers["If-None-Match"] = cached_tag

    response = requests.get(url, headers=headers)

    if response.status_code == 304:
        print("304 Not Modified: 使用缓存内容。")
    elif response.status_code == 200:
        print("200 OK: 下载新资源并更新缓存。")
        # 更新本地缓存逻辑
        print("Last-Modified:", response.headers.get("Last-Modified"))
    else:
        print("其他状态码:", response.status_code)

    print(response.headers)
    print(response.text)

# 测试接口
# GET：/cache
# Returns a 304 if an If-Modified-Since header or If-None-Match is present. Returns the same as a GET otherwise.
# parameter：If-Modified-Since, If-None-Match; (header)
# response: 200(cached response); 304(Modified)
url = "https://httpbin.org/cache"
fetch_resource_with_cache(url)